/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.acd;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.filefilter.SuffixFileFilter;

import net.bioteam.appweb.Config;
import net.bioteam.appweb.TestConfig;
import net.bioteam.appweb.model.Parameter;
import net.bioteam.appweb.model.ParameterType;
import net.bioteam.appweb.model.Program;
import net.bioteam.appweb.model.ProgramBuilder;
import net.bioteam.appweb.model.XmlToProgram;
import net.bioteam.appweb.submit.CtrlException;
import net.bioteam.appweb.submit.DefaultCommandLineGenerator;
import net.bioteam.appweb.submit.ProgramValueBean;
import net.bioteam.appweb.submit.UnfinishedJob;
import net.sf.ehcache.CacheManager;

import junit.framework.TestCase;

//TODO multiple selection "" in comparsion with vdef issue
public class AcdToProgramTest extends TestCase {

	UnfinishedJob job=new UnfinishedJob();
	public void setUp() throws Exception {
		TestConfig.init();
		job.setWorkdir(TestConfig.topdir);
		
	}

	public void tearDown() throws Exception {
		TestConfig.shutdown();
	}

	public AcdToProgramTest() {
		super();
	}

	public AcdToProgramTest(String test) {
		super(test);
	}

	public static void main(String args[]) {
		AcdToProgramTest test = new AcdToProgramTest("testRangeType");
		junit.textui.TestRunner.run(test);
	}
	

	/**
	 * Make sure we can parse all known acd from EMBOSS Two Acd files have
	 * error.
	 * 
	 * @throws IOException
	 */
	public void testAll() throws IOException {

		File acddir = new File(AcdToProgram.EMBOSS_ACD);

		List<String> programs = new LinkedList<String>();

		programs.addAll(Arrays.asList(acddir
				.list(new SuffixFileFilter(".acd"))));

		for (String filename : programs) {

			String programName = filename.substring(0, filename.length() - 4);
			Program program = null;

			try {
				System.out.println("program="+programName);
				program = AcdToProgram.toProgram(programName);
				assertNotNull(program);
			} catch (Exception e) {
					System.err.println();
					e.printStackTrace();
					fail("error for " + programName);
				
			}

		}

	}

	/**
	 * make sure we can handle default value properly
	 */
	public void testBooleanType() {
		ProgramBuilder.cache.remove("abiview");
		

		try {
			
			Program abiview = ProgramBuilder.getProgram("abiview");

			Parameter param = abiview.getParameter("separate");
			assertEquals("0", param.getDeaultValue());

			param = abiview.getParameter("sequence");
			assertEquals("1", param.getDeaultValue());

			ProgramValueBean bean = new ProgramValueBean(abiview);
			bean.setValue("infile", "myinfile");
			DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
			String cmdline = generator.generateScript((ProgramValueBean) bean
					.myClone(), job);
			String expected = "abiview -auto -stdout  -infile myinfile  -outseq outfile10.txt  -graph png -goutfile goutfile15";
			
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));

			bean.setValue("sequence", "0");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			expected = "abiview -auto -stdout  -infile myinfile  -outseq outfile10.txt  -graph png -goutfile goutfile15  -nosequence";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			bean.setValue("sequence", "1");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			expected = "abiview -auto -stdout  -infile myinfile  -outseq outfile10.txt  -graph png -goutfile goutfile15";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));

			bean.setValue("separate", "1");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			expected = "abiview -auto -stdout  -infile myinfile  -outseq outfile10.txt  -graph png -goutfile goutfile15  -separate";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			bean.setValue("separate", "0");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			expected = "abiview -auto -stdout  -infile myinfile  -outseq outfile10.txt  -graph png -goutfile goutfile15";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			ProgramBuilder.cache.remove("merger");
			Program merger = ProgramBuilder.getProgram("merger");
			bean = new ProgramValueBean(merger);
			bean.setValue("asequence", "a");
			bean.setValue("bsequence", "b");
			Parameter p=merger.getParameter("outfile_aaccshow");
			bean.setValue("outfile_aaccshow", "0");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			//System.out.println(cmdline);
			expected = "merger -auto -stdout  -asequence a  -bsequence b  -outfile outfile30_align.txt  -outseq outfile35.txt";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			ProgramBuilder.cache.remove("diffseq");
			Program diffseq = ProgramBuilder.getProgram("diffseq");
			bean = new ProgramValueBean(diffseq);
			bean.setValue("asequence", "a");
			bean.setValue("bsequence", "b");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			//System.out.println(cmdline);
			expected = "diffseq -auto -stdout  -asequence a  -bsequence b  -outfile outfile25.txt  -aoutfeat outfile30_featout.gff  -boutfeat outfile35_featout.gff";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));

		} catch (Exception e) {

			e.printStackTrace();
			fail(e.getMessage());
		}

	}
	
	/**
	 * make sure we can handle default value properly
	 */
	public void testOutputType() {
	
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		try {
			//align type.
			ProgramBuilder.cache.remove("merger");
			Program merger = ProgramBuilder.getProgram("merger");
			ProgramValueBean bean = new ProgramValueBean(merger);
			bean.setValue("asequence", "a");
			bean.setValue("bsequence", "b");
			Parameter p=merger.getParameter("outfile_aaccshow");
			bean.setValue("outfile_aaccshow", "0");
			String cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			//System.out.println(cmdline);
			String expected = "merger -auto -stdout  -asequence a  -bsequence b  -outfile outfile30_align.txt  -outseq outfile35.txt";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			//with feauture output
			ProgramBuilder.cache.remove("diffseq");
			Program diffseq = ProgramBuilder.getProgram("diffseq");
			bean = new ProgramValueBean(diffseq);
			bean.setValue("asequence", "a");
			bean.setValue("bsequence", "b");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			//System.out.println(cmdline);
			expected = "diffseq -auto -stdout  -asequence a  -bsequence b  -outfile outfile25.txt  -aoutfeat outfile30_featout.gff  -boutfeat outfile35_featout.gff";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));

			//graph
			ProgramBuilder.cache.remove("dottup");
			Program dottup = ProgramBuilder.getProgram("dottup");
			bean = new ProgramValueBean(dottup);
			bean.setValue("asequence", "a");
			bean.setValue("bsequence", "b");
			bean.setValue("graph_gtitle", "v00294 vs j01636");
			bean.setValue("graph_gdesc", "long description with space");
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			//System.out.println(cmdline);
			expected = "dottup -auto -stdout  -asequence a  -bsequence b  -graph png -goutfile goutfile25  -gdesc 'long description with space'  -gtitle 'v00294 vs j01636'  -xygraph png -goutfile goutfile30";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		} catch (Exception e) {

			e.printStackTrace();
			fail(e.getMessage());
		}

	}

	public void testMiscFeature() {
		
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		try {
			//align type.
			ProgramBuilder.cache.remove("makenucseq");
			Program p = ProgramBuilder.getProgram("makenucseq");
			ProgramValueBean bean = new ProgramValueBean(p);
			bean.setValue("codonfile", "Epseae.cut");
			bean.setValue("amount", "2");
			bean.setValue("length", "150");
			bean.setValue("outseq_osformat", "fasta");
			bean.setValue("outseq", "seq.txt");
			
			String cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			//System.out.println(cmdline);
			String expected = "makenucseq -auto -stdout  -codonfile Epseae.cut  -amount 2  -length 150  -outseq seq.txt";
			assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			ProgramBuilder.cache.remove("ehmmbuild");
			p = ProgramBuilder.getProgram("ehmmbuild");
			bean = new ProgramValueBean(p);
			bean.setValue("alignfile", "globins50.msf");
			bean.setValue("nhmm", "globins50");
			bean.setValue("hmmfile", "globin.hmm");
			bean.setValue("strategy", "D");
			
			cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
			System.out.println(cmdline);
			//String expected = "makenucseq -auto -stdout  -codonfile Epseae.cut  -amount 2  -length 150  -outseq seq.txt";
			//assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
			
			
		} catch (Exception e) {

			e.printStackTrace();
			fail(e.getMessage());
		}

	}
	
	public void testSeqType() throws Exception {
		ProgramBuilder.cache.remove("plotcon");
		Program plotcon = ProgramBuilder.getProgram("plotcon");
		Parameter p=plotcon.getParameter("sequences_sbegin");
		assertNotNull(p);
		
		ProgramValueBean bean = new ProgramValueBean(plotcon);
		bean.setValue("sequences", "seq");
		bean.setValue("sequences_sbegin", "5");
		bean.setValue("sequences_sreverse", "1");
		bean.setValue("sequences_sformat", "msf");
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		System.out.print(cmdline);
		String expected = "plotcon -auto -stdout  -sequences seq  -sbegin 5  -sreverse   -sformat msf  -graph png -goutfile goutfile20";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
	}
	
	public void testFloatType() throws Exception {
		ProgramBuilder.cache.remove("cirdna");
		Program cirdna = ProgramBuilder.getProgram("cirdna");

		Parameter p = cirdna.getParameter("originangle");
		assertEquals(90.0, Double.parseDouble(p.getDeaultValue()));
		assertEquals(0, Integer.parseInt(p.getScalemin().getValue()));
		assertEquals(360, Integer.parseInt(p.getScalemax().getValue()));

		p = cirdna.getParameter("textheight");
		assertEquals(1.0, Double.parseDouble(p.getDeaultValue()));

		ProgramValueBean bean = new ProgramValueBean(cirdna);
		bean.setValue("infile", "myinfile");
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		String expected = "cirdna -auto -stdout  -infile myinfile  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("originangle", "270");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -originangle 270  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		//default value does not contribue to command line
		bean.setValue("originangle", "90");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
	}
	
	public void testIntegerType() throws Exception {
		ProgramBuilder.cache.remove("complex");
		Program complex = ProgramBuilder.getProgram("complex");

		Parameter p = complex.getParameter("jmin");
		assertEquals(4, Integer.parseInt(p.getDeaultValue()));
		assertEquals(2, Integer.parseInt(p.getScalemin().getValue()));
		assertEquals(20, Integer.parseInt(p.getScalemax().getValue()));

		p = complex.getParameter("lwin");
		assertEquals(100, Integer.parseInt(p.getDeaultValue()));
		assertNull(p.getScalemax());
		
		//print is internal function. Make sure the program generator generate  a command with name print1 instead of print
		p=complex.getParameter("print1");
		assertEquals("0", p.getDeaultValue());
		
		ProgramValueBean bean = new ProgramValueBean(complex);
		bean.setValue("sequence", "myinfile");
				bean.setValue("print1", "0");
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		String expected = "complex -auto -stdout  -sequence myinfile  -outfile outfile50.outfile  -outseq outfile60.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("jmin", "8");
		bean.setValue("lwin", "50");
		generator = new DefaultCommandLineGenerator();
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "complex -auto -stdout  -sequence myinfile  -lwin 50  -jmin 8  -outfile outfile50.outfile  -outseq outfile60.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));

	}

	
	public void testStringType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("dbifasta");
		
		//test minlegth and maxlength
		program = ProgramBuilder.getProgram("dbifasta");
		p = program.getParameter("dbname");
		assertEquals(2, p.getCtrls().size());
		
		bean = new ProgramValueBean(program);
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		
		
		
		bean.setValue("dbname", "dbname");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "dbifasta -auto -stdout  -dbname dbname  -outfile outfile65.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean = new ProgramValueBean(program);
		bean.setValue("dbname", "d");
		generator = new DefaultCommandLineGenerator();
		try
		{
			cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
			fail("minlenght does not work");
		} catch (CtrlException e)
		{
			//normal
		}
		bean.setValue("dbname", "ddddddddddddddddddddddddddddddddddddddd");
		try
		{
			cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
			fail("maxlength does not work");
		} catch (CtrlException e)
		{
			//normal
		}
		
		
		//test word
		ProgramBuilder.cache.remove("ehmmbuild");
		program = ProgramBuilder.getProgram("ehmmbuild");
		p = program.getParameter("nhmm");
		assertEquals(1, p.getCtrls().size());
		assertNull(p.getDeaultValue());
		
		bean = new ProgramValueBean(program);
		bean.setValue("alignfile", "alignfile");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		bean.setValue("nhmm", "xxx");
		
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "ehmmbuild -auto -stdout  -alignfile alignfile  -nhmm xxx  -hmmfile outfile100.hmmfile  -o outfile105.o  -cfile outfile110.cfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("nhmm", "x xx");
		try
		{
			cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
			fail("word restriction does not work");
		} catch (CtrlException e)
		{
			//normal
		}
		
		ProgramBuilder.cache.remove("wobble");
		program = ProgramBuilder.getProgram("wobble");
		p = program.getParameter("bases");
		assertEquals("GC", p.getDeaultValue());
		
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seq");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "wobble -auto -stdout  -sequence seq  -graph png -goutfile goutfile20  -outfile outfile25.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("bases", "GC");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "wobble -auto -stdout  -sequence seq  -graph png -goutfile goutfile20  -outfile outfile25.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("bases", "GCG");
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "wobble -auto -stdout  -sequence seq  -bases GCG  -graph png -goutfile goutfile20  -outfile outfile25.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));

	}

	
	public void testRangeType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("eprimer3");
		
		//test minlegth and maxlength
		program = ProgramBuilder.getProgram("eprimer3");
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seqfile");
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "eprimer3 -auto -stdout  -sequence seqfile  -outfile outfile295.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("targetregion", "target");
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "eprimer3 -auto -stdout  -sequence seqfile  -targetregion @target  -outfile outfile295.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		

	}

	public void testCodonType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("backtranseq");
		
		//with default value
		program = ProgramBuilder.getProgram("backtranseq");
		assertEquals("Ehuman.cut", program.getParameter("cfile").getDeaultValue());
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seqfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "backtranseq -auto -stdout  -sequence seqfile  -outfile outfile15.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("cfile", "Ehuman.cut");
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "backtranseq -auto -stdout  -sequence seqfile  -outfile outfile15.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("cfile", "Ehuman1.cut");
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "backtranseq -auto -stdout  -sequence seqfile  -cfile Ehuman1.cut  -outfile outfile15.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		
		//without default value
		ProgramBuilder.cache.remove("codcmp");
		program = ProgramBuilder.getProgram("codcmp");
		bean = new ProgramValueBean(program);
		bean.setValue("first", "first.cut");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "codcmp -auto -stdout  -first first.cut  -outfile outfile15.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
	}
	
	public void testDatafileType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("banana");
		
		//with default value
		program = ProgramBuilder.getProgram("banana");
		assertEquals("Eangles_tri.dat", program.getParameter("anglesfile").getDeaultValue());
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seqfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "banana -auto -stdout  -sequence seqfile  -graph png -goutfile goutfile15";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("anglesfile", "Eangles_tri.dat");
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "banana -auto -stdout  -sequence seqfile  -graph png -goutfile goutfile15";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("anglesfile", "Eangles_tri1.dat");
		//we can generate command if the string is null
		cmdline = generator.generateScript((ProgramValueBean) bean
				.myClone(), job);
		expected = "banana -auto -stdout  -sequence seqfile  -anglesfile Eangles_tri1.dat  -graph png -goutfile goutfile15";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		
		//with default value
		program = ProgramBuilder.getProgram("restover");
		assertNull( program.getParameter("datafile").getDeaultValue());
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seqfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "restover -auto -stdout  -sequence seqfile  -outfile outfile95.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("datafile", "datafile1");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "restover -auto -stdout  -sequence seqfile  -datafile datafile1  -outfile outfile85.outfile";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
	}
	public void testMatrixType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("cons");
		
		//with default value
		program = ProgramBuilder.getProgram("cons");
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seqfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cons -auto -stdout  -sequence seqfile  -outseq outfile30.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("datafile", "matrix");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cons -auto -stdout  -sequence seqfile  -datafile matrix  -outseq outfile30.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
	}
	
	public void testListType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("cirdna");
		
		//with default value
		program = ProgramBuilder.getProgram("cirdna");
		assertEquals(ParameterType.SingleSelectionType, program.getParameter("blocktype").getType());
		assertEquals("Filled", program.getParameter("blocktype").getDeaultValue());
		assertEquals(3, program.getParameter("blocktype").getVlist().getValue().size());
		assertEquals("Open", program.getParameter("blocktype").getVlist().getValue().get(0));
		assertEquals("Open blocks", program.getParameter("blocktype").getVlist().getLabel().get(0));
		
		bean = new ProgramValueBean(program);
		bean.setValue("infile", "myinfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("blocktype", "Open");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -blocktype Open  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("blocktype", "Filled");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		
		ProgramBuilder.cache.remove("transeq");
		
		//with default value
		program = ProgramBuilder.getProgram("transeq");
		assertEquals(ParameterType.MultipleSelectionType, program.getParameter("frame").getType());
		List list=(List)program.getParameter("frame").getDefaultAsList();
		assertEquals("1", list.get(0));
		assertEquals(1, list.size());
		assertEquals(9, program.getParameter("frame").getVlist().getValue().size());
		assertEquals("1", program.getParameter("frame").getVlist().getValue().get(0));
		assertEquals("1", program.getParameter("frame").getVlist().getLabel().get(0));
		
		bean = new ProgramValueBean(program);
		bean.setValue("sequence", "seqfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "transeq -auto -stdout  -sequence seqfile  -outseq outfile40.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("frame", "1");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "transeq -auto -stdout  -sequence seqfile  -outseq outfile40.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("frame", "\"1 2\"");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "transeq -auto -stdout  -sequence seqfile  -frame \"1 2\"  -outseq outfile40.txt";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
	}

	
	public void testSelectionType() throws Exception {
		Program program;
		Parameter p;
		DefaultCommandLineGenerator generator = new DefaultCommandLineGenerator();
		String cmdline, expected;
		ProgramValueBean bean;
		ProgramBuilder.cache.remove("cirdna");
		
		//with default value
		program = ProgramBuilder.getProgram("cirdna");
		assertEquals(ParameterType.SingleSelectionType, program.getParameter("posticks").getType());
		assertEquals("Out", program.getParameter("posticks").getDeaultValue());
		assertEquals(2, program.getParameter("posticks").getVlist().getValue().size());
		assertEquals("In", program.getParameter("posticks").getVlist().getValue().get(0));
		assertEquals("In", program.getParameter("posticks").getVlist().getLabel().get(0));
		
		bean = new ProgramValueBean(program);
		bean.setValue("infile", "myinfile");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("posticks", "Out");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		bean.setValue("blocktype", "In");
		cmdline = generator.generateScript((ProgramValueBean) bean.myClone(), job);
		expected = "cirdna -auto -stdout  -infile myinfile  -blocktype In  -graphout png -goutfile goutfile105";
		assertEquals(expected.replaceAll("\\s+", " "), cmdline.trim().replaceAll("\\s+", " "));
		
		
		
	}


	/**
	 * Test how attribute value is parsed in acd file.
	 */
	public void testAttributeValue() {
		String myvalue = null;
		String newvalue = null;

		// literal value
		myvalue = "abc";
		newvalue = AcdToProgram.processAttributeValue(myvalue);
		assertEquals("abc", newvalue);

		// quote is stripped.
		myvalue = "\"abc\"";
		newvalue = AcdToProgram.processAttributeValue(myvalue);
		assertEquals("abc", newvalue);

		// escape quote \" is converted to "
		myvalue = "\"abc\\\"\"";
		newvalue = AcdToProgram.processAttributeValue(myvalue);
		assertEquals("abc\"", newvalue);

		// espscape \\ is converted to \
		myvalue = "\"abc\\\"\\\\\"";
		newvalue = AcdToProgram.processAttributeValue(myvalue);
		assertEquals("abc\"\\", newvalue);

	}

}
